using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace DataEntity
{
	public class T_Reports_Parameter : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlBoolean		_new_Cell;
			private SqlInt32		_serial_NO, _reports_ID, _parameter_ID;
			private SqlString		_default_Values, _group_Name, _control_ID, _parameter_Name, _parameter_Title, _control_Type;
		#endregion


		public T_Reports_Parameter()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Reports_Parameter_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@iParameter_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _parameter_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@iReports_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _reports_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sParameter_Name", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _parameter_Name));
				cmdToExecute.Parameters.Add(new SqlParameter("@sParameter_Title", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _parameter_Title));
				cmdToExecute.Parameters.Add(new SqlParameter("@iSerial_NO", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _serial_NO));
				cmdToExecute.Parameters.Add(new SqlParameter("@sControl_Type", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _control_Type));
				cmdToExecute.Parameters.Add(new SqlParameter("@sControl_ID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _control_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sDefault_Values", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _default_Values));
				cmdToExecute.Parameters.Add(new SqlParameter("@sGroup_Name", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _group_Name));
				cmdToExecute.Parameters.Add(new SqlParameter("@bNew_Cell", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _new_Cell));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Reports_Parameter::Insert::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Reports_Parameter_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@iParameter_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _parameter_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@iReports_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _reports_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sParameter_Name", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _parameter_Name));
				cmdToExecute.Parameters.Add(new SqlParameter("@sParameter_Title", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _parameter_Title));
				cmdToExecute.Parameters.Add(new SqlParameter("@iSerial_NO", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _serial_NO));
				cmdToExecute.Parameters.Add(new SqlParameter("@sControl_Type", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _control_Type));
				cmdToExecute.Parameters.Add(new SqlParameter("@sControl_ID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _control_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sDefault_Values", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _default_Values));
				cmdToExecute.Parameters.Add(new SqlParameter("@sGroup_Name", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _group_Name));
				cmdToExecute.Parameters.Add(new SqlParameter("@bNew_Cell", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _new_Cell));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Reports_Parameter::Update::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Reports_Parameter_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@iParameter_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _parameter_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Reports_Parameter::Delete::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Reports_Parameter_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Reports_Parameter");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@iParameter_ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _parameter_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_parameter_ID = (Int32)toReturn.Rows[0]["Parameter_ID"];
					_reports_ID = (Int32)toReturn.Rows[0]["Reports_ID"];
					_parameter_Name = (string)toReturn.Rows[0]["Parameter_Name"];
					_parameter_Title = (string)toReturn.Rows[0]["Parameter_Title"];
					_serial_NO = (Int32)toReturn.Rows[0]["Serial_NO"];
					_control_Type = (string)toReturn.Rows[0]["Control_Type"];
					_control_ID = (string)toReturn.Rows[0]["Control_ID"];
					_default_Values = (string)toReturn.Rows[0]["Default_Values"];
					_group_Name = (string)toReturn.Rows[0]["Group_Name"];
					_new_Cell = (bool)toReturn.Rows[0]["New_Cell"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Reports_Parameter::SelectOne::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Reports_Parameter_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Reports_Parameter");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Reports_Parameter::SelectAll::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlInt32 Parameter_ID
		{
			get
			{
				return _parameter_ID;
			}
			set
			{
				SqlInt32 parameter_IDTmp = (SqlInt32)value;
				if(parameter_IDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Parameter_ID", "Parameter_ID can't be NULL");
				}
				_parameter_ID = value;
			}
		}


		public SqlInt32 Reports_ID
		{
			get
			{
				return _reports_ID;
			}
			set
			{
				SqlInt32 reports_IDTmp = (SqlInt32)value;
				if(reports_IDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Reports_ID", "Reports_ID can't be NULL");
				}
				_reports_ID = value;
			}
		}


		public SqlString Parameter_Name
		{
			get
			{
				return _parameter_Name;
			}
			set
			{
				SqlString parameter_NameTmp = (SqlString)value;
				if(parameter_NameTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Parameter_Name", "Parameter_Name can't be NULL");
				}
				_parameter_Name = value;
			}
		}


		public SqlString Parameter_Title
		{
			get
			{
				return _parameter_Title;
			}
			set
			{
				SqlString parameter_TitleTmp = (SqlString)value;
				if(parameter_TitleTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Parameter_Title", "Parameter_Title can't be NULL");
				}
				_parameter_Title = value;
			}
		}


		public SqlInt32 Serial_NO
		{
			get
			{
				return _serial_NO;
			}
			set
			{
				SqlInt32 serial_NOTmp = (SqlInt32)value;
				if(serial_NOTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Serial_NO", "Serial_NO can't be NULL");
				}
				_serial_NO = value;
			}
		}


		public SqlString Control_Type
		{
			get
			{
				return _control_Type;
			}
			set
			{
				SqlString control_TypeTmp = (SqlString)value;
				if(control_TypeTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Control_Type", "Control_Type can't be NULL");
				}
				_control_Type = value;
			}
		}


		public SqlString Control_ID
		{
			get
			{
				return _control_ID;
			}
			set
			{
				SqlString control_IDTmp = (SqlString)value;
				if(control_IDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Control_ID", "Control_ID can't be NULL");
				}
				_control_ID = value;
			}
		}


		public SqlString Default_Values
		{
			get
			{
				return _default_Values;
			}
			set
			{
				SqlString default_ValuesTmp = (SqlString)value;
				if(default_ValuesTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Default_Values", "Default_Values can't be NULL");
				}
				_default_Values = value;
			}
		}


		public SqlString Group_Name
		{
			get
			{
				return _group_Name;
			}
			set
			{
				SqlString group_NameTmp = (SqlString)value;
				if(group_NameTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Group_Name", "Group_Name can't be NULL");
				}
				_group_Name = value;
			}
		}


		public SqlBoolean New_Cell
		{
			get
			{
				return _new_Cell;
			}
			set
			{
				SqlBoolean new_CellTmp = (SqlBoolean)value;
				if(new_CellTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("New_Cell", "New_Cell can't be NULL");
				}
				_new_Cell = value;
			}
		}
		#endregion
	}
}
